package com.juxfe.bookmanagement.service;


import com.juxfe.bookmanagement.mapper.UserMapper;
import com.juxfe.bookmanagement.model.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserMapper userMapper;

    @Override
    public User login(String username, String password) {
        return userMapper.verifyLogin(username, password);
    }

    @Override
    public boolean register(User user) {
        // 检查用户名是否已存在
        if (checkUsernameExists(user.getUsername())) {
            return false;
        }
        // 插入新用户
        int result = userMapper.insert(user);
        return result > 0;
    }

    @Override
    public boolean checkUsernameExists(String username) {
        User user = userMapper.findByUsername(username);
        return user != null;
    }

    @Override
    public User getUserById(Integer id) {
        return userMapper.findById(id);
    }

    @Override
    public boolean updateUser(User user) {
        int result = userMapper.update(user);
        return result > 0;
    }
}